package com.store.controller;

import com.store.entity.Address;
import com.store.service.AddressService;
import com.store.util.JsonResult;
import io.swagger.annotations.Api;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@Api("收获地址控制层")
@RestController
@RequestMapping("/address")
public class AddressController {

    @DubboReference(interfaceClass = AddressService.class,version = "1.0.0",timeout = 5000)
    private AddressService addressService;

    @PostMapping("/add")
    public JsonResult addAddress(Address address,HttpSession session){
        Integer uid = (Integer)session.getAttribute("uid");
        String username = (String)session.getAttribute("username");
        addressService.addAddress(address,uid,username);
        return new JsonResult(200);
    }

    @PostMapping("/getAll")
    public JsonResult getAll(HttpSession session){
        Integer uid = (Integer)session.getAttribute("uid");
        List<Address> addressList = addressService.getAllAddress(uid);
        return new JsonResult(200,addressList);
    }

    @PostMapping("/delete")
    public JsonResult delete(Integer aid){
        addressService.deleteAddress(aid);
        return new JsonResult(200);
    }
}
